package com.five.bi.controller;

import com.five.bi.model.request.DataSourceRequest;
import com.five.bi.model.DatasourceInfo;
import com.five.bi.service.DatasourceInfoService;
import com.five.bi.utils.JsonData;
import com.five.bi.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class DataSourceShowController {
    @Autowired
    private DatasourceInfoService datasourceInfoService;

    /**
     * 获取数据源列表
     * @return
     */
    @GetMapping("/datasource/list")
    public JsonData getDataSourceList(){
        List<DataSourceRequest> dataSourceList = datasourceInfoService.getDataSourceList();
        return JsonData.buildSuccess(dataSourceList);
    }

    /**
     * 获取当前数据源下的库
     * @param dataType
     * @return
     */
    @PostMapping("/db/list")
    public JsonData getDbList(@RequestParam("dataType") String dataType){
        List<DatasourceInfo> dblist = datasourceInfoService.dbList(dataType);
        return JsonData.buildSuccess(dblist);
    }
    /**
     * 模糊查询数据源下库
     */
    @PostMapping("/db/like")
    public JsonData getDbLike(@RequestBody Map map){
        String dataType = MapUtil.converterOfString(map.get("dataType"));
        String search = '%' + MapUtil.converterOfString(map.get("search")) + '%';
        List<DatasourceInfo> dblike = datasourceInfoService.dbLike(dataType,search);
        return JsonData.buildSuccess(dblike);
    }
}
